package dxn.nl.file.biz.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import dxn.nl.file.biz.po.ConsumerInfoPo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface ConsumerInfoDao extends BaseMapper<ConsumerInfoPo> {
    @Update("update consumer_info\n" +
            "set consumer_count=consumer_count+1,consumer_status=1,version_no=version_no+1,consumer_refresh_time=now(),consumer_thread=#{consumerThread}\n" +
            "where id=#{id} and type_code=#{typeCode} and version_no=#{versionNo} " +
            "and consumer_thread is null and consumer_count<16 and consumer_status =0")
    int lockId(@Param("id")long id, @Param("typeCode")int typeCode,@Param("versionNo")long versionNo, @Param("consumerThread")String consumerThread);

    @Update("update consumer_info\n" +
            "set consumer_status=#{consumerStatus},version_no=version_no+1,consumer_refresh_time=now(),consumer_thread=null" +
            ",end_time=now(),consumer_count=consumer_count+1 \n" +
            "where id=#{id} and type_code=#{typeCode} and consumer_thread=#{consumerThread} and consumer_status =1")
    int unlockId(@Param("id") long id, @Param("typeCode")int typeCode,@Param("consumerStatus") int consumerStatus,@Param("consumerThread") String consumerThread);

    @Update("update consumer_info \n" +
            "set consumer_status=0,consumer_thread = null \n" +
            "where consumer_refresh_time>=DATE_SUB(NOW(), INTERVAL 1 DAY) and consumer_thread !=null and consumer_status =1 and consumer_refresh_time < DATE_SUB(NOW(), INTERVAL 5 MINUTE)")
    int removeDeadlockId();

    @Update("update consumer_info \n" +
            "set consumer_refresh_time=now() \n" +
            "where id=#{id} and type_code=#{typeCode} and consumer_thread=#{consumerThread} and consumer_status =1")
    int refreshLockId(@Param("id")long id, @Param("typeCode")int typeCode,@Param("consumerThread") String consumerThread);
}
